<template>
  <g-form-wrap :title="title" :on-save-btn-click="onSaveBtnClick">
    <el-form
      ref="ruleForm"
      :model="formData"
      :rules="formRules"
      label-position="left"
      label-width="140px"
      inline
    >
      <div class="w-full">
        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="调研机构名称" prop="researchOrgName">
              <el-input
                v-model="formData.researchOrgName"
                placeholder="请输入调研机构名称"
                class="edit-item"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号状态" prop="researchDatastatus">
              <g-select
                v-model="formData.researchDatastatus"
                placeholder="请选择账号状态"
                class="edit-item"
                :options="[{'id': 1, 'name': '有效'}, {'id': 2, 'name': '无效'}]"
                :filterable="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>
  </g-form-wrap>
</template>

<script lang="ts" setup>
import { reactive, ref, toRefs } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import type { Loading } from '@/components/Dialog/types'
import { useRoute, useRouter } from 'vue-router'
import elv from '@/utils/elValidation'
import { getResearchOrgByIdApi, addResearchOrgApi, updateResearchOrgApi } from '@/api/admin'
import { Page } from '@/types/storeModule'
import { assign, cloneDeep, omit } from 'lodash-es'

const route = useRoute()
const router = useRouter()
const isEditing = ref(false)
const ruleForm = ref<FormInstance>()
let options = ref<any>([])
let formData = ref(<
  {
    researchOrgId: string | number
    researchOrgName: string
    researchDatastatus: string | number
  }
>{
    researchOrgId: 0,
    researchOrgName: '',
    researchDatastatus: 1
})
const formRules = reactive<FormRules>({
    researchOrgName: [{ required: true, message: '请输入调研机构名称', trigger: 'change' }]
})
const id = route.query.id as string | number
const title = route.meta.title as string
isEditing.value = id > 0 ? true : false
// 编辑时
if (isEditing.value) {
    const getResearchOrgByIdFunc = async () => {
        const data = await getResearchOrgByIdApi({ id })
        console.log('data---', data)
        formData.value = cloneDeep(omit(data, ['History', 'history', 'repeatKeys']))
    }
    getResearchOrgByIdFunc()
}
// 保存提交
const onSaveBtnClick = (loading: Loading) => {
    ruleForm.value?.validate((valid) => {
        if (valid) {
            loading(true)
            if (isEditing.value) {
                // console.log('formData---update', formData);
                updateResearchOrgApi(formData.value).then((res) => {
                    // console.log('res---update', res);
                    if(res*1 === 200)
                        ElMessage.success('编辑成功')
                })
            } else {
                // console.log('formData---add', formData);
                addResearchOrgApi(formData.value).then((res) => {
                    // console.log('res---add', res);
                    if(res*1 === 201)
                        ElMessage.success('添加成功')
                })
            }
            setTimeout(() => {
                loading(false)
                router.back()
            }, 100)
        } else {
            return false
        }
    })
}
</script>
